
package org.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/*
 * This class displays all the course numbers in a particular department of
 * SFSU. Like for Computer Science it will display all grad and undergrad courses
 * */
public class DisplayCourseNumberPresentActivity extends ListActivity {

	private String result = "";
	
	ArrayList<String> stringList = new ArrayList<String>();
	private int counter = 0;

	@SuppressWarnings("unused")
	final private static String[] FRUITS = { "First", "Second", "Third",
			"Forth" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//Interacting with the remote database using HTTP request
		
			HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				"http://hci.cs.sfsu.edu/~fall2010.03/Includes/GetAllCourseNumbersPresent.php");
		try {

			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();


			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();

			result = sb.toString();
		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}

		try {
			JSONArray jArray = new JSONArray(result);

			for (int i = 0; i < jArray.length(); i++) {
				JSONObject json_data = jArray.getJSONObject(i);
				Log.i("log_tag", " Title: "
						+ json_data.getString("Course_Number")

				);

				stringList.add(json_data.getString("Course_Number"));
			
				counter++;
			
			}
		} catch (JSONException e) { 
		
			System.out.println("NO RECORDS FOUND");
		}

	
		setListAdapter(new ArrayAdapter<String>(this,
				R.layout.displaycoursenumberpresentlayout, stringList));
	
		ListView listView = getListView();

		listView.setOnItemClickListener(new OnItemClickListener() {

		//Once the item is clicked the Activity is switched to DisplayCourseNumberPresentActivity	
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				
				Intent i = new Intent(DisplayCourseNumberPresentActivity.this,
						test.class);
				i.putExtra("CourseNumber", ((TextView) view).getText());
				startActivity(i);

			}

		});

	}
}